/* 
头条搜索列表id 
 */
function searchList(keyword,page){
	let url = 'https://www.toutiao.com/api/search/content/';
	offset = page * 20;
	result = '';
	$.ajax({
		url:url,
		type:"get",
		async:false,
		data:{
			aid: 24,
			app_name: "web_search",
			offset: offset,
			format: "json",
			keyword: keyword,
			autoload: true,
			count: 20,
			en_qc: 1,
			cur_tab: 1,
			from: "search_tab",
			pd: "synthesis",
			timestamp: (new Date()).getTime()/1000
		},
		success:function(data){
			if(data){
				result = data;
			}
		},
		error:function(){
			layer.msg('头条文章获取异常');
		}
	})
	return result;
}

/* 
 头条content_id数据抛送
 */
function ttContentIdSubmit(tags,data){
	result = false;
	$.ajax({
		url:'https://dd.youdnr.com/api/mate/list/store',
		type:"post",
		async:false,
		 data:{
			 data:data,
			 platform:1,
			 tags:tags
		 },
		success:function(data){
			console.log(data);
			if(data.code == 200){
				result = true;
			}
		},
		error:function(){
			layer.msg('抛送异常');
		}
	})
	return result;
}


//头条content_id获取
function ttContentId(){
	result = '';
	$.ajax({
		url:'https://dd.youdnr.com/api/mate/contentid?platform=1',
		type:"get",
		async:false,
		success:function(data){
			if(data.result){
				result = data.result;
			}
		},
		error:function(){
			layer.msg('抛送异常');
		}
	})
	return result;
}

/* 
 获取头条单条详细数据
 */
function ttDetail(content_id){
	ar_link  = 'https://www.toutiao.com/i' + content_id + '/';
	var result= {};
	result.main_info = {};
	$.ajax({
	    url:ar_link,
		async:false,
	    success:function (res) {
			tags = '';
			//匹配标签 tags: [{"name":"陈数"},{"name":"服装"},{"name":"美女"},{"name":"工作这一年"},{"name":"体育"}],
			
			tag_ = res.match(/tags: \[(\S*)\]/);
			if(tag_ instanceof Array){
				tag_ = tag_[1];
			}else{
				tag_ = false;
			}
			if(tag_){
				tmp_tags = JSON.parse('[' + tag_ + ']');
				if(tmp_tags.length > 0){
					for(t of tmp_tags){
						tags = tags + ' ' + t.name;
					}
				}
			}
			pub_time = '';
			//匹配发布时间  time: '2019-01-14 18:22:27'
			tmp_pub_time = res.match(/time: \'((\S|\s)*?)\'/);
			if(tmp_pub_time[1]){
				pub_time = tmp_pub_time[1];
			}
			
			
			//————
	        var ar=res.split('articleInfo:');
	        ar=ar[1].split('commentInfo: ');
	        ar=ar[0].trim();
			//匹配标题
	        var title=ar.split(' title: \'');
	        title=title[1].split(';\'.slice(6, -6)');
	        title=title[0];
			
			title = title.replace(/\&quot;{0,1}/g, "");
			
			//匹配内容
	        var content=ar.split('content: \'');
	        content=content[1].split(';\'.slice(6, -6)');
	        content=content[0];
	        content=decodeUnicode(content);
	        $('html body').append('<div class="ar"></div><div class="ar1"></div>');
	        $('.ar').html(title);
	        $('.ar1').html(content);
	        var tit=$('.ar').text().trim();
	        var con=$('.ar1').html();
	        var contents=[];
	        var con_len=$('.ar1 p').length;
	        var con_arr=[];
	        for( var k=0;k<con_len;k++){
	            var con_p=$('.ar1 p').eq(k).text().trim();
	            if(con_p!=''){
	                con_arr.push(con_p);
	            }
	        }
	        con_len=con_arr.length;
	        var data=$('.ar1').find('img').eq(0);
	        var img_len=data.prevObject.length;
	        var max=con_len>img_len?con_len:img_len;
			ind = 1;
	        for( var k=0;k<max;k++){
	            if(con_arr[k]){
	                contents.push({ "type": 1, "content": con_arr[k],'order_by':ind });
	            }
	            if(data.prevObject[k]){
	                var attr=data.prevObject[k].attributes;
	                var src='http://'+attr[1].name+'/'+attr[2].name+'/'+attr[3].name+'/'+attr[4].name;
	                src = src.replace(/%&quot;/g, "");
	                contents.push({ "type": 2, "content": src,'order_by':ind });
	            }
				ind++;
	        }
			console.log(title)
	        result.main_info.title = title;
			result.main_info.tags = tags;
			result.main_info.pub_time = pub_time;
	        result.contents =  contents; 
			result.platform =  1;
			result.content_id = content_id;
			console.log(result);
			
		
	        $('.ar').remove();
	        $('.ar1').remove();
		}
	})
	return result;
}

/* 
 解码
 */
function decodeUnicode(str) {
	str = str.replace(/\\/g, "%");
	//转换中文
	str = unescape(str);
	return str;
}

/* 
 数据提交
 */
function ttItemSubmit(data){
	result = false;
	$.ajax({
		url:'https://dd.youdnr.com/api/mate/store',
		type:"post",
		async:false,
	    data:data,
		success:function(data){
			if(data.code == 200){
				result = true;
			}
		},
		error:function(){
			layer.msg('抛送异常');
		}
	})
	return result;
	
}